package sit.engreal.leetcode.problems100;

/**
 * <a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-array/"> 26. 删除有序数组中的重复项 </a>
 */
public class LeetCode26 {
    static class Solution {
        public int removeDuplicates(int[] nums) {
            int i = 0; // [0, i]
            int j = 1;  // [j, )
            while (j < nums.length) {
                if (nums[i] < nums[j]) {
                    nums[i + 1] = nums[j];
                    i++;
                }
                j++;
            }
            return i + 1;
        }
    }

    public static void main(String[] args) {
        final Solution solution = new Solution();

        {
            int[] input = {1, 1, 2};
            int[] expect = {1, 2};
            int k = solution.removeDuplicates(input); // 调用
            assert k == expect.length;
            for (int i = 0; i < k; i++) {
                assert input[i] == expect[i];
            }
        }
        {
            int[] input = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
            int[] expect = {0, 1, 2, 3, 4};
            int k = solution.removeDuplicates(input); // 调用
            assert k == expect.length;
            for (int i = 0; i < k; i++) {
                assert input[i] == expect[i];
            }
        }

    }
}
